package com.jhopesoft.framework.dao.entity.dictionary;
// Generated 2016-11-3 11:11:36 by Hibernate Tools 5.2.0.Beta1

import java.util.HashSet;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;

import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.GenericGenerator;

import com.alibaba.fastjson.JSONObject;
import com.jhopesoft.framework.dao.entity.viewsetting.FovGridnavigateschemedetail;

/**
 * FNumbergroup generated by hbm2java
 */
@Entity
@DynamicUpdate
@Table(name = "f_numbergroup", uniqueConstraints = @UniqueConstraint(columnNames = "title"))
public class FNumbergroup implements java.io.Serializable {

  private static final long serialVersionUID = 2739399643536106231L;
  private String numbergroupid;
  private String title;
  private String remark;
  private Set<FNumbergroupdetail> FNumbergroupdetails = new HashSet<FNumbergroupdetail>(0);
  private Set<FovGridnavigateschemedetail> fovGridnavigateschemedetails = new HashSet<FovGridnavigateschemedetail>(0);

  public FNumbergroup() {}

  // 给自定义函数用的
  public String genFunctionExpression() {

    StringBuilder sb = new StringBuilder();
    sb.append("( case");
    for (FNumbergroupdetail detail : getFNumbergroupdetails()) {
      sb.append(
          " when (" + detail.genFunctionExpression() + ") then '" + (detail.getTitle()).replaceAll("'", "") + "'");
      sb.append("\r\n");
    }
    sb.append(" end )");
    return sb.toString();

  }

  // 给导航用的
  public String genExpression(String fieldname) {
    StringBuilder sb = new StringBuilder();
    sb.append("( case");
    for (FNumbergroupdetail detail : getFNumbergroupdetails()) {
      sb.append(" when (" + detail.genExpression(fieldname) + ") then " + (detail.getOrderno() + 1000));
      sb.append("\r\n");
    }
    sb.append(" end )");
    return sb.toString();
  }

  public String getDetailTitle(Integer orderId) {
    for (FNumbergroupdetail detail : getFNumbergroupdetails()) {
      if (detail.getOrderno().equals(orderId)) return detail.getTitle();
    }
    return "未定义";
  }

  public JSONObject genJsonObject() {
    JSONObject object = new JSONObject();
    object.put("title", title);
    object.put("numbergroupid", numbergroupid);
    object.put("remark", remark);
    return object;

  }

  @GenericGenerator(name = "generator", strategy = "uuid.hex")
  @Id
  @GeneratedValue(generator = "generator")
  @Column(name = "numbergroupid", unique = true, nullable = false, length = 40)
  public String getNumbergroupid() {
    return this.numbergroupid;
  }

  public void setNumbergroupid(String numbergroupid) {
    this.numbergroupid = numbergroupid;
  }

  @Column(name = "title", unique = true, nullable = false, length = 50)
  public String getTitle() {
    return this.title;
  }

  public void setTitle(String title) {
    this.title = title;
  }

  @Column(name = "remark", length = 200)
  public String getRemark() {
    return this.remark;
  }

  public void setRemark(String remark) {
    this.remark = remark;
  }

  @OneToMany(fetch = FetchType.LAZY, mappedBy = "FNumbergroup")
  public Set<FNumbergroupdetail> getFNumbergroupdetails() {
    return this.FNumbergroupdetails;
  }

  public void setFNumbergroupdetails(Set<FNumbergroupdetail> FNumbergroupdetails) {
    this.FNumbergroupdetails = FNumbergroupdetails;
  }

  @OneToMany(fetch = FetchType.LAZY, mappedBy = "FNumbergroup")
  public Set<FovGridnavigateschemedetail> getFovGridnavigateschemedetails() {
    return this.fovGridnavigateschemedetails;
  }

  public void setFovGridnavigateschemedetails(Set<FovGridnavigateschemedetail> fovGridnavigateschemedetails) {
    this.fovGridnavigateschemedetails = fovGridnavigateschemedetails;
  }

}
